import tkinter as tk
from tkinter import ttk

colors = {
    "Background": "#F8F8FF",
    "Block": "#DEB887",
    "Monster": "#006400",
    "Coin": "#EEE8AA",
    "QuestionBlock": "#ADFF2F",
    "Castle": "#9932CC",
    "Gate":"#808080"
}

element_mapping = {
    "Background": 0,
    "Block": 1,
    "Monster": 2,
    "Coin": 3,
    "QuestionBlock": 4,
    "Castle":7,
    "Gate":8
}

def create_grid(rows, cols):
    grid = []
    for i in range(rows):
        if i >= rows - 5:
            row = [1] * cols
        else:
            row = [0] * cols
        grid.append(row)
    return grid

def on_click(row, col):
    current_color = selected_color.get()
    grid[row][col] = element_mapping[current_color]
    canvas.itemconfig(cells[row][col], fill=colors[current_color])

def save_to_file(grid):
    with open('MYMAP.map', 'w') as f:
        for row in grid:
            line = ",".join(str(cell) for cell in row)
            f.write(line + "\n")

def generate_file():
    save_to_file(grid)
    print("文件已生成！")

def update_grid():
    global rows, cols, grid, cells

    rows = int(rows_entry.get())
    cols = int(cols_entry.get())

    grid = create_grid(rows, cols)

    canvas.config(scrollregion=(0, 0, cols * cell_width, rows * cell_height))
    canvas.delete("all")
    cells = []
    for i in range(rows):
        row = []
        for j in range(cols):
            x1 = j * cell_width
            y1 = i * cell_height
            x2 = x1 + cell_width
            y2 = y1 + cell_height
            rect = canvas.create_rectangle(x1, y1, x2, y2, fill=colors["Block"] if i >= rows - 5 else colors["Background"])
            rect_func = lambda event, row=i, col=j: on_click(row, col)
            canvas.tag_bind(rect, "<Button-1>", rect_func)
            row.append(rect)
        cells.append(row)

root = tk.Tk()

root.title("地图设计器")
selected_color = tk.StringVar(root)
selected_color.set("White")

rows = 24
cols = 200
cell_width = 20
cell_height = 20

grid = create_grid(rows, cols)

canvas_frame = tk.Frame(root)
canvas_frame.pack()

canvas = tk.Canvas(canvas_frame, width=cols * cell_width, height=rows * cell_height)
canvas.pack(side="left")

scrollbar = ttk.Scrollbar(root, orient="horizontal", command=canvas.xview)
scrollbar.pack(side="bottom", fill="x")

canvas.configure(xscrollcommand=scrollbar.set)

canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

scrollbar.configure(command=canvas.xview)

cells = []
for i in range(rows):
    row = []
    for j in range(cols):
        x1 = j * cell_width
        y1 = i * cell_height
        x2 = x1 + cell_width
        y2 = y1 + cell_height
        rect = canvas.create_rectangle(x1, y1, x2, y2, fill=colors["Block"] if i >= rows - 5 else colors["Background"])
        rect_func = lambda event, row=i, col=j: on_click(row, col)
        canvas.tag_bind(rect, "<Button-1>", rect_func)
        row.append(rect)
    cells.append(row)

button_frame = tk.Frame(root)
button_frame.pack()

generate_button = tk.Button(button_frame, text="确定", command=generate_file)
generate_button.pack()

grid_frame = tk.Frame(root)
grid_frame.pack()

rows_label = tk.Label(grid_frame, text="行数:")
rows_label.pack(side="left")

rows_entry = tk.Entry(grid_frame)
rows_entry.insert(0, str(rows))
rows_entry.pack(side="left")

cols_label = tk.Label(grid_frame, text="列数:")
cols_label.pack(side="left")

cols_entry = tk.Entry(grid_frame)
cols_entry.insert(0, str(cols))
cols_entry.pack(side="left")

color_selection_frame = tk.Frame(root)
color_selection_frame.pack()

for color in colors.keys():
    radio_button = tk.Radiobutton(color_selection_frame, text=color, variable=selected_color, value=color)
    radio_button.pack(side="left")

update_button = tk.Button(grid_frame, text="更新", command=update_grid)
update_button.pack(side="left")

root.mainloop()